﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class DAO_HeThong
    {
        public static List<DTO_HeThong> SelectChucNang(int id_nhom)
        {
            List<DTO_HeThong> listCN = new List<DTO_HeThong>();
            try
            {
                string s = "Proc_ChucNang";
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = ketnoi.Cmb();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = s;
                cmd.Parameters.Add("@ID_Nhom", SqlDbType.NVarChar).Value = id_nhom;
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                DataTable dt = new DataTable();
                da.Fill(dt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DTO_HeThong CN = new DTO_HeThong();
                    CN.ID_Nhom = int.Parse(dt.Rows[i]["ID_Nhom"].ToString());
                    CN.Pr = bool.Parse(dt.Rows[i]["pr"].ToString());
                    CN.ID_ChucNang = int.Parse(dt.Rows[i]["ID_ChucNang"].ToString());
                    CN.TenChucNang = dt.Rows[i]["TenChucNang"].ToString();
                    CN.TenForm = dt.Rows[i]["TenForm"].ToString();
                    listCN.Add(CN);
                }
                ketnoi.Cmb().Close();
                return listCN;
            }
            catch (Exception e)
            {
                return listCN;
            }
        }
        public static List<DTO_HeThong> SelectChucNang_TheoNhom(int id_nhom)
        {
            List<DTO_HeThong> listCN = new List<DTO_HeThong>();
            try
            {
                string s = "Proc_ChucNang_TheoNhom";
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = ketnoi.Cmb();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = s;
                cmd.Parameters.Add("@ID_Nhom", SqlDbType.NVarChar).Value = id_nhom;
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                DataTable dt = new DataTable();
                da.Fill(dt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DTO_HeThong CN = new DTO_HeThong();
                    CN.ID_Nhom = int.Parse(dt.Rows[i]["ID_Nhom"].ToString());
                    CN.Pr = bool.Parse(dt.Rows[i]["pr"].ToString());
                    CN.ID_ChucNang = int.Parse(dt.Rows[i]["ID_ChucNang"].ToString());
                    CN.TenChucNang = dt.Rows[i]["TenChucNang"].ToString();
                    CN.TenForm = dt.Rows[i]["TenForm"].ToString();
                    listCN.Add(CN);
                }
                ketnoi.Cmb().Close();
                return listCN;
            }
            catch (Exception e)
            {
                return listCN;
            }
        }
        public static bool updateQuyen(string id_nhom, string id_chucnang)
        {
            try
            {
                SqlConnection cn = ketnoi.Cmb();
                string s = "proc_updateQuyen";
                SqlCommand cmd1 = new SqlCommand(s, cn);
                cmd1.CommandType = CommandType.StoredProcedure;
                cmd1.Parameters.AddWithValue("@ID_Nhom", id_nhom);
                cmd1.Parameters.AddWithValue("@ID_ChucNang", id_chucnang);
                if (cn.State == ConnectionState.Closed)
                    cn.Open();
                cmd1.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

    }
}
